/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.multi.client.components;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.macdadi.core.client.domain.ExtendedProjectInfo;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class ProjectListing extends Composite
{
    protected FlexTable table;
    public static final String CSS_CLASS = "macdadi-ProjectListing";

    public ProjectListing()
    {
        VerticalPanel vp = new VerticalPanel();

        table = new FlexTable();
        table.setWidget(0, 0, new HTML("<b>Name</b>"));
        table.setWidget(0, 1, new HTML("<b>Link</b>"));
        table.setWidget(0, 2, new HTML("<b>Stats</b>"));
        
        vp.add(table);

        initWidget(vp);
        setStyleName(CSS_CLASS);
    }

    public void displayProjects(ExtendedProjectInfo[] projects)
    {
        int numRows = table.getRowCount();

        for (int i = numRows - 1; i > 0; i--) {
            table.removeRow(i);
        }
        
        ExtendedProjectInfo p;
        String linkURL;
        for (int i = 0; i < projects.length; i++ ) {
            p = projects[i];
            table.setWidget(i+1, 0, new HTML(p.getName()));
            /* The ProjectServlet is mapped to the "/p*" path, and the "up" parameter is set to the urlPath */
            linkURL = "p?up=" + p.getUrlPath();
            table.setWidget(i+1, 1, new HTML("<a href='" + linkURL + "'>" + linkURL + "</a>"));
            table.setWidget(i+1, 2, new HTML(getStatsText(p)));
            if ( p.isAllowCopy() )
                table.setWidget(i+1, 3, new LinkLabel("copy"));
        }
    }

    protected String getStatsText(ExtendedProjectInfo p)
    {
        String text = getCountText(p.getNumProjectMembers(), "member", "members");
        text += ", " + getCountText(p.getNumGoals(), "goal", "goals");
        text += ", " + getCountText(p.getNumOptions(), "option", "options");
        
        return text;
    }

    protected String getCountText(int num, String singular, String plural)
    {
        switch ( num ) {
            case 0:
                return "no " + plural;
            case 1:
                return "1 " + singular;
            default:
                return num + " " + plural;
        }                 
    }
}
